/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gr.forth.ics.continuoustopk.index;

import gr.forth.ics.continuoustopk.Item;
import gr.forth.ics.continuoustopk.Constants;
import gr.forth.ics.continuoustopk.Query;
import gr.forth.ics.continuoustopk.Term;

/**
 *
 * @author Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 */
public interface QueryVisitedListener extends CountListener {
    public void onQueryVisited(Query q, Item c, Term term, int groupIndex);
    
    public int count();

    public QueryVisitedListener counter = new QueryVisitedListener() {
        private int count = 0;

        public void onQueryVisited(Query q, Item c, Term term, int skyband) {
            count++;
        }

        public int count() {
            try {
                return count;
            } finally {
                count = 0;
            }
        }
    };

    public QueryVisitedListener onConstraints = new QueryVisitedListener() {
        private int count = 0;

        public void onQueryVisited(Query q, Item c, Term term, int skyband) {
            double max = 0;
            for (int i = 0; i < c.termCount(); i++) {
                double ci = c.getWeight(i);
                if (c.getTerm(i) != term && ci > max) {
                    max = ci;
                }
            }
            double cs = c.itemScore();
            double ci = c.getWeight(term);
            if (Constants.primaryConstraint.shouldVisit(q.minScore(), q.getWeight(term), cs, ci, max) &&
                        Constants.secondaryConstraint.shouldVisit(q.minScore(), q.getWeight(term), cs, ci, max)) {
                count++;
            }
        }

        public int count() {
            try {
                return count;
            } finally {
                count = 0;
            }
        }
    };

    public QueryVisitedListener onPrimary = new QueryVisitedListener() {
        private int count = 0;

        public void onQueryVisited(Query q, Item c, Term term, int skyband) {
            double max = 0;
            for (int i = 0; i < c.termCount(); i++) {
                double ci = c.getWeight(i);
                if (c.getTerm(i) != term && ci > max) {
                    max = ci;
                }
            }
            double cs = c.itemScore();
            double ci = c.getWeight(term);
            if (Constants.primaryConstraint.shouldVisit(q.minScore(), q.getWeight(term), cs, ci, max)) {
                count++;
            }
        }

        public int count() {
            try {
                return count;
            } finally {
                count = 0;
            }
        }
    };
    
    public QueryVisitedListener onSecondary1 = new QueryVisitedListener() {
        private int count = 0;

        public void onQueryVisited(Query q, Item c, Term term, int skyband) {
            double max = 0;
            for (int i = 0; i < c.termCount(); i++) {
                double ci = c.getWeight(i);
                if (c.getTerm(i) != term && ci > max) {
                    max = ci;
                }
            }
            double cs = c.itemScore();
            double ci = c.getWeight(term);
            if (Constants.secondaryConstraint.shouldVisit(q.minScore(), q.getWeight(term), cs, ci, max)) {
                count++;
            }
        }

        public int count() {
            try {
                return count;
            } finally {
                count = 0;
            }
        }
    };

}
